import * as types from "../types/suggestTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import { isEmpty } from "../utils/utils";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";

export const SUGGEST_LOADING = "SUGGEST_LOADING";
export const SUGGEST_DONE = "SUGGEST_DONE";
export const SUGGEST_ERROR = "SUGGEST_ERROR";

export function submitSuggest(title, content) {
  return dispatch => {
    if (isEmpty(title)) {
      toastShort("请输入意见");
      return;
    }
    if (isEmpty(content)) {
      toastShort("请输入详细描述");
      return;
    }
    dispatch({ type: types.SUGGEST_LOADING });
    NetWorkUtil.post(ApiConstants.SUBMIT_SUGGEST, { title, content })
      .then(res => {
        toastShort("意见提交成功");
        dispatch({ type: types.SUGGEST_DONE });
      })
      .catch(error => {
        toastShort("意见提交失败");
        dispatch({ type: types.SUGGEST_ERROR, error });
      });
  };
}

export function inputTitel(title) {
  return dispatch => {
    dispatch({ type: types.SUGGEST_EDIT_TITLE, title });
  };
}

export function inputContent(content) {
  return dispatch => {
    dispatch({ type: types.SUGGEST_EDIT_CONTENT, content });
  };
}

export function editSuggestUnmount() {
  return dispatch => {
    dispatch({ type: types.SUGGEST_UNMOUNT });
  };
}
